package com.abbas.smartCity.ui.parkingWhere;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.Death;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.BannerImageAdapter2;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseFragment;
import com.abbas.smartCity.bean.parkingWhereBean.ParkingBannerList;
import com.abbas.smartCity.bean.parkingWhereBean.ParkingList;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MListView;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class ParkingMainFragment extends BaseFragment {

    private Banner<String, BannerImageAdapter2<String>> banner;
    private EditText searchEt;
    private MListView lv;
    private ListCommAdapter<ParkingList.RowsBean, ListCommAdapter.NewsHolder> parkingListAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_parking_main;
    }

    @Override
    protected void initView() {
        banner = (Banner) rootView.findViewById(R.id.banner);
        searchEt = (EditText) rootView.findViewById(R.id.search_et);
        lv = (MListView) rootView.findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
        final List<ParkingBannerList.RowsBean> parkingBannerListRows = Death.parkingBannerList.getRows();
//        banner.setImageLoader(App.urlLoader)
//                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
//                .setImages(parkingBannerListRows.stream()
//                        .map(ParkingBannerList.RowsBean::getAdvImg)
//                        .collect(Collectors.toList()))
//                .setBannerTitles(parkingBannerListRows.stream()
//                        .map(ParkingBannerList.RowsBean::getAdvTitle)
//                        .collect(Collectors.toList()))
//                .startAutoPlay();

        banner.setAdapter(new BannerImageAdapter2<>(
                        parkingBannerListRows.stream()
                                .map(ParkingBannerList.RowsBean::getAdvImg)
                                .collect(Collectors.toList())
                ))
                .setIndicator(new CircleIndicator(requireContext()))
                .start();
        getParkingList(null);
    }

    @Override
    protected void initEvent() {
        searchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    final String targetStr = searchEt.getText().toString();
                    getParkingList(targetStr);
                    return true;
                }
                return false;
            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final ParkingList.RowsBean item = parkingListAdapter.getItem(position);
                final Intent intent = new Intent(requireActivity(), ParkingDetailActivity.class);
                intent.putExtra("id", item.getId());
                startActivity(intent);
            }
        });
    }

    private void getParkingList(String parkName) {
        api.getParkingList(parkName).enqueue(new ICallback<ParkingList>() {
            @Override
            public void onSuccess(Call<ParkingList> call, ParkingList data) {
                if (parkingListAdapter == null) {
                    parkingListAdapter = new ListCommAdapter<ParkingList.RowsBean, ListCommAdapter.NewsHolder>(
                            data.getRows(),
                            ListCommAdapter.NewsHolder.class,
                            R.layout.news_item
                    ) {
                        @Override
                        protected void show(ParkingList.RowsBean item, View convertView, NewsHolder viewHolder, int position, Context context) {
                            Glide.with(context)
                                    .load(App.getBaseUrl() + item.getImgUrl())
                                    .into(viewHolder.im);
                            viewHolder.title.setText(item.getParkName());
                            viewHolder.sub.setText("地址：" + item.getAddress());
                            viewHolder.tag1.setText("距离：" + item.getDistance());
                            viewHolder.tag2.setText("空位个数：" + item.getVacancy());
                        }
                    };
                    lv.setAdapter(parkingListAdapter);
                } else {
                    parkingListAdapter.resetData(data.getRows());
                }
            }
        });
    }
}